package hbase;

import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.coprocessor.AggregateImplementation;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.net.URI;

/**
 * Created by spark on 16-10-18.
 */
public class CoprocessorTest extends Configured implements Tool{
    @Override
    public int run(String[] args) throws Exception {

        FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"), getConf());
        Path path = new Path(fs.getUri() + Path.SEPARATOR + "jars/RegionObserveExample.jar");
//        fs.copyFromLocalFile(new Path("file:///home/spark/projects/hadoop-test/target/hadoop-test-1.0-SNAPSHOT.jar"), path);

        Connection conn = ConnectionFactory.createConnection(getConf());
        HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();

        admin.disableTable(TableName.valueOf("test"));

        HTableDescriptor descriptor =
                admin.getTableDescriptor(TableName.valueOf("test"));

//        HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf("test"));
//        descriptor.addFamily(new HColumnDescriptor("info"));
        descriptor.addCoprocessor(RegionObserveExample.class.getCanonicalName(),
                path, 1021, null);
        descriptor.addCoprocessor(AggregateImplementation.class.getCanonicalName(),
                null, 1023, null);

//        descriptor.setValue("joe", "|org.apache.Hadoop.hbase.joe.AggregateImplementation||");
        admin.modifyTable(TableName.valueOf("test"), descriptor);


        admin.enableTable(TableName.valueOf("test"));
//        admin.createTable(descriptor);

        System.out.println(admin.getTableDescriptor(TableName.valueOf("test")));

        return 0;
    }

    public static void main(String[] args) throws Exception {
        int exitCode = ToolRunner.run(new CoprocessorTest(), args);
        System.exit(exitCode);
    }
}
